/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.service.impl;

import com.tesam.gedeon.dao.ProviderAccountDao;
import com.tesam.gedeon.entity.ProviderAccount;
import com.tesam.gedeon.service.ProviderAccountService;
import java.text.SimpleDateFormat;
import java.util.List;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author joelpacheco
 */
@Service
public class ProviderAccountServiceImpl implements ProviderAccountService {

    @Autowired
    private ProviderAccountDao providerAccountDao;

    @Override
    public int save(ProviderAccount providerAccount) {
        return providerAccountDao.save(providerAccount);
    }

    @Override
    public int delete(ProviderAccount providerAccount) {
        return providerAccountDao.delete(providerAccount);
    }

    @Override
    public int update(ProviderAccount providerAccount) {
        return providerAccountDao.update(providerAccount);
    }

    @Override
    public ProviderAccount getById(int providerAccountId) {
        return providerAccountDao.getById(providerAccountId);
    }

    @Override
    public List<ProviderAccount> getAll() {
        return providerAccountDao.getAll();
    }

    @Override
    public List<ProviderAccount> getAllByLimit(int startLimit, int endLimit, String dataToSort, String directionOfOrder) {
        return providerAccountDao.getAllByLimit(startLimit, endLimit, dataToSort, directionOfOrder);
    }

    @Override
    public List<ProviderAccount> searchByName(ProviderAccount providerAccount, int satartLimit, int endLimit) {
        return providerAccountDao.searchByName(providerAccount, satartLimit, endLimit);
    }

    @Override
    public int getCountSearchByName(ProviderAccount providerAccount) {
        return providerAccountDao.getCountSearchByName(providerAccount);
    }

    @Override
    public int getCountTotal() {
        return providerAccountDao.getCountTotal();
    }

    @Override
    public String getJsonDataFromArrayList(List<ProviderAccount> listaDeProviderAccount, int cantidadRegistros, int moduleId) {
        JSONArray array = null;
        JSONObject json = null;

        SimpleDateFormat format = null;

        try {

            json = new JSONObject();
            array = new JSONArray();

            if (listaDeProviderAccount.size() > 0) {


                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // Date date = sdf.parse("2004-07-24 09:45:52.189");

                for (ProviderAccount providerAccount : listaDeProviderAccount) {
                    JSONArray arrayPut = new JSONArray();

                    arrayPut.put(providerAccount.getId());
                    arrayPut.put(providerAccount.getCode());
                    arrayPut.put(providerAccount.getName());
                    arrayPut.put(providerAccount.getProvider().getName());
                    if (providerAccount.getDateCreated() != null) {
                        arrayPut.put((format.format(providerAccount.getDateCreated())));
                    } else {
                        arrayPut.put(providerAccount.getDateCreated());
                    }
                    // arrayPut.put("<a href=\"#delete\" data-toggle=\"modal\" class=\" btn yellow\" value=\""+rol.getId()+"\">Eliminar</a>");
                    arrayPut.put("<a class=\"btn mini blue\" href=\"/GedeonMvc/provideraccount/view?id=" + providerAccount.getId() + "&module=" + moduleId + "\">"
                            + "<i class=\"icon-share\"></i>View</a>"
                            + " <a class=\"btn mini green\" href=\"/GedeonMvc/provideraccount/edit?id=" + providerAccount.getId() + "&module=" + moduleId + "\">"
                            + "<i class=\"icon-edit\"></i>Editar</a>"
                            + " <a href=\"#delete\" data-toggle=\"modal\" class=\"btn mini red\" value=\"" + providerAccount.getId() + "\"><i class=\"icon-trash\"></i>Eliminar</a>");
                    array.put(arrayPut);
                }
            }

            json.put("iTotalRecords", cantidadRegistros);
            json.put("iTotalDisplayRecords", cantidadRegistros);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.println("Exception getJsonDataFromArrayList:" + e.getMessage());
            return null;
        }
    }
}
